import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/primeng';
import { Authority } from './fb-authority.model';
import { MdlSnackbarService } from '@angular-mdl/core';
import { FormAuthorityService } from './fb-authority.service';

@Component({
  selector: 'fb-authority',
  templateUrl: './fb-authority.component.html',
  styleUrls: ['./fb-authority.component.scss'],
  providers: [FormAuthorityService]
})
export class FormAuthorityComponent implements OnInit {
  procs: TreeNode[]; //过程
  selectedProc: TreeNode;
  loadingProc: boolean = true;

  actParts: TreeNode[]; //环节参与者
  selectedActPart: TreeNode;
  loadingActParts: boolean = true;

  formScorllHeight: string = '300px';
  loading: true;

  forms: Authority[];//文书
  fields: Authority[];//字段

  //全部文书可见选项
  allFormVisiable: number = 0;
  allFieldRead: number = 0;
  allFieldWrite: number = 0;
  selectedForm: Authority;
  constructor(
    private mdlSnackbarService: MdlSnackbarService,
    private formAuthorityService: FormAuthorityService) { }

  ngOnInit() {
    this.formScorllHeight = $(document).height() - 230 + 'px';
    this.formAuthorityService.loadBizProcTree().subscribe(procs => {
      this.loadingProc = false;
      this.procs = procs;
      let temp = [];
      this.recursionSuggestion(temp, procs);
      this.selectedProc = temp[0];
      this.procNodeSelect(null);
    });

  }
  /**表单权限保存 */
  saveAuthority() {
    let forms = this.forms;
    let actDefId = this.selectedActPart.data['actDefId'];
    let partId = this.selectedActPart.data['partId'];
    this.formAuthorityService.saveAythority(forms, actDefId, partId).subscribe(data => {
      this.mdlSnackbarService.showToast("表单权限保存成功");
    });
  }

  /**表单读写权限的变更 */
  formAuthorityChange(rowData) {
    this.allFormVisiable = 1;
    this.forms.forEach(form => {
      //变更全选状态
      if (!form.read) this.allFormVisiable = 0;
      //默认将所有字段设置为可编辑
      if (form.id == rowData.id) {
        if (form.read) {
          form.read = 1;
          form.fields.forEach(field => {
            field.read = 0;
            field.write = 1;
            this.allFieldRead = 0;
            this.allFieldWrite = 1;
          });
        } else {
          form.read = 0;
          form.fields.forEach(field => {
            field.read = 0;
            field.write = 0;
            this.allFieldRead = 0;
            this.allFieldWrite = 0;
          })
        }
      }
    })
  }
  /**递归可选流程 */
  recursionSuggestion(suggestions: any[], nodes: TreeNode[]) {
    nodes.forEach(node => {
      if (node.selectable) {
        suggestions.push(node);
      }
      if (node.children)
        this.recursionSuggestion(suggestions, node.children);
    });
  }
  /**表单选中切换 */
  onFormRowSelect($event) {
    this.allFieldRead = 1;
    this.allFieldWrite = 1;
    let rowData = $event.data;
    this.forms.forEach(form => {
      if (form.id == rowData.id) {
        this.fields = form.fields;
        this.fields.forEach(field => {
          if (field.read == 0) this.allFieldRead = 0;
          if (field.write == 0) this.allFieldWrite = 0;
        })
        return;
      }
    })
  }

  /** 修改全部文书可见权限 */
  changeAllFormVisiable($event) {
    this.forms.forEach(form => {
        if (this.allFormVisiable) {form.read = 1; } else {form.read = 0; }
        form.fields.forEach(field => {
          field.read = 0;
          field.write = 1;
      });
    });
  }

  /** 点击表头修改全部字段可见/可编辑权限 */
  changeAllFieldAuthority(type) {
    let formId = this.fields[0].seniorId;
    this.forms.forEach(form => {
      if (form.id == formId) {
        form.read = 1;
        form.fields.forEach(field => {
            if ('read' == type) {
                field.read == 1 ? field.read = 0 : field.read = 1;
                field.write = 0;
                this.allFieldRead == 1 ? this.allFieldRead = 1 : this.allFieldRead = 0;
                this.allFieldWrite = 0;
            } else if ('write' == type) {
                field.read = 0;
                field.write == 1 ? field.write = 0 : field.write = 1;
                this.allFieldWrite == 1 ? this.allFieldWrite = 1 : this.allFieldWrite = 0;
                this.allFieldRead = 0;
            }
        });
      }
    });
  }

  /** 点击字段 */
  fieldAuthorityChange(rowData) {
      debugger
    this.forms.forEach(form => {
      if (rowData.seniorId == form.id) {
        form.read = 1;
        return;
      }
    });
    if (rowData.read === true) {
      rowData.read = 1;
      rowData.write = 0;
      this.allFieldWrite = 0;
    } else {
      rowData.read = 0;
    }
    if (rowData.write === true) {
      rowData.write = 1;
      rowData.read = 0;
      this.allFieldRead = 0;
    } else {
      rowData.write = 0;
    }
  }

  /**流程选中 */
  procNodeSelect(event) {
      debugger
    let selectProcId = this.selectedProc['id'].replace("PROC_", "");

    //加载流程对应环节与参与者
    this.formAuthorityService.loadActParts(selectProcId).subscribe(parts => {
      this.loadingActParts = false;
      this.actParts = parts;
      this.selectedActPart = this.actParts[0];
      //加载流程对应的表单字段数据
      this.formAuthorityService.loadProcFormFileds(selectProcId).subscribe(formfields => {
        this.forms = formfields;
        this.fields = [];
        if (formfields.length && formfields.length > 0) {
          this.selectedForm = formfields[0];
          this.fields = this.selectedForm.fields;
          this.actPartNodeSelect(null);//节点选中
        }
      });
    });
  }

  /** 选中节点参与者 */
  actPartNodeSelect(event) {
      debugger
    //获取part对应的表单字段权限
    if (!this.selectedActPart) return;
    let actDefId = this.selectedActPart.data.actDefId;
    let partId = this.selectedActPart.data.partId;
    this.formAuthorityService.loadFormFieldAuth(actDefId, partId).subscribe(auths => {
      let temp = this.forms;
      temp.forEach(form => {
        form.read = 0;
        if (auths.length > 0) {
          auths.forEach(authForm => {
            //表单权限
            if (form.id == authForm.id) {
              form.read = authForm.read;
              //字段权限
              form.fields.forEach(field => {
                field.read = field.write = 0;
                authForm.fields.forEach(authField => {
                  if (field.id == authField.id) {
                    field.read = authField.read;
                    field.write = authField.write;
                  }
                });
              });
            }
          });
        } else {
          //无数据
          form.read = 0;
          form.fields.forEach(field => {
            field.read = field.write = 0;
          });
        }
      });
      this.forms = temp;
    });
  }
}
